<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;

class HeighLightController extends Controller
{
    public function getHeighLightList(){

        try{
            $results = DB::table('aboutcompany')->where('type','=','2')->paginate(10);
         //dd($results);

            return view('Admin.heighlightlist',['results'=> $results]);

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }
    public function addHeightLight(Request $request){

        try{
            $input['title'] = $request->input('title');
            $input['other'] = $request->input('other');
            $input['editorValue'] = $request->input('editorValue');
            $file = $request->file('aboutcompany_image');
            $aboutcompany_image = $this->heighLightImgUpload($file);
            $validator = Validator::make($input, [
                'title' => 'required',
                'other' => 'required',
                'editorValue'=> 'required'
            ]);

            if ($validator->fails()) {
                return $validator->messages();
            }

            $data['title'] = $input['title'];
            $data['other'] = $input['other'];
            $data['type'] = 2;
            $data['aboutcompany_content'] = $input['editorValue'];
            $data['aboutcompany_image'] = $aboutcompany_image;
            $data['time'] = date('Y-m-d H:i:s');


            $result = DB::table('aboutcompany')->insert($data);
            if($result){

                return redirect('/admin/getHeighLightList');
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

    public function doUpdateHeighLight(Request $request){

        try{
            $input['title'] = $request->input('title');
            $input['other'] = $request->input('other');
            $input['editorValue'] = $request->input('editorValue');
            $input['aboutcompany_id'] = $request->input('aboutcompany_id');

            $validator = Validator::make($input, [
                'aboutcompany_id' => 'required',
                'title' => 'required',
                'other' => 'required',
                'editorValue'=> 'required'
            ]);

            if ($validator->fails()) {
                return $validator->messages();
            }

            $data['title'] = $input['title'];
            $data['other'] = $input['other'];
            $data['aboutcompany_content'] = $input['editorValue'];
            $data['time'] = date('Y-m-d H:i:s');

            $file = $request->file('aboutcompany_image');
            if($file){
                $aboutcompany_image = $this->heighLightImgUpload($file);
                $data['aboutcompany_image'] = $aboutcompany_image;
            }

            $result = DB::table('aboutcompany')->where('aboutcompany_id','=',$input['aboutcompany_id'])->update($data);
            if($result){

                return redirect('/admin/getHeighLightList');
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }
    public function updateHeighLight(Request $request){

        try{
            $input['aboutcompany_id'] = $request->input('aboutcompany_id');
             $aboutcompany_id = $input['aboutcompany_id'];

        $results = DB::table('aboutcompany')->where('aboutcompany_id','=',$aboutcompany_id)->get();
        //dd($results);
        return view('Admin.heighlightupdate',['result' => $results]);

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

    public function deleteHeighLight(Request $request){

        try{
            $data['aboutcompany_id'] = $request->input('aboutcompany_id');
            $results = DB::table('aboutcompany')->where('aboutcompany_id','=',$data['aboutcompany_id'])->delete();

            if($results){

                return redirect('/admin/getHeighLightList');
            }

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }

    }

    public function heighLightImgUpload($file){

            if($file->isValid()){

                $mimetype = $file->getClientMimeType();
                $originalName = $file->getClientOriginalName();

                $ext = $file->getClientOriginalExtension();
                $realPath = $file->getRealPath();

                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;

                $bool = Storage::disk('uploads')->put($filename,file_get_contents($realPath));



                $path = "/../public/uploads/";
                $url = '/uploads/'.$filename;

               return $url;
        }

    }

}
